/**
 * Copyright (c) 2015-2019, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jfinal.plugin.activerecord.sql;

import java.util.Collection;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.TemplateException;
import com.jfinal.template.expr.ast.ExprList;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.ParseException;
import com.jfinal.template.stat.Scope;

/**
 * @author Golly
 */
public class OrParasDirective extends Directive {

	@Override
	public void exec (Env env, Scope scope, Writer writer) {
		SqlPara sqlPara = (SqlPara) scope.get(SqlKit.SQL_PARA_KEY);

		if (sqlPara == null) {
			throw new TemplateException("#orparas directive invoked by getSqlPara(...) method only", this.location);
		}

		Object[] paraArray = this.exprList.evalExprList(scope);
		int flag = 0; // OR参数个数
		String paraName = null;

		for (int i = 0; i < paraArray.length; i++) {
			Object para = paraArray[i];

			if (i == 0) {

				if (!(para instanceof String) || StrKit.isBlank((String) para)) {
					throw new ParseException("The first parameter must be a string", this.location);
				}

				paraName = (String) para;
				continue;
			}

			if (para == null) { // 优先处理参数为Null的情况
				flag = this.addPara(sqlPara, para, paraName, writer, flag);
			} else if (para instanceof Collection<?>) { // 处理参数为Collection的情况
				Collection<?> paraCollection = (Collection<?>) para;

				for (Object obj : paraCollection) {
					flag = this.addPara(sqlPara, obj, paraName, writer, flag);
				}

			} else if (para.getClass().isArray()) { // 处理参数为Array的情况
				Object[] objArray = (Object[]) para;

				for (Object object : objArray) {
					flag = this.addPara(sqlPara, object, paraName, writer, flag);
				}

			} else { // 处理参数为一般类型的情况
				flag = this.addPara(sqlPara, para, paraName, writer, flag);
			}

		}

	}

	@Override
	public void setExprList (ExprList exprList) {

		if (exprList.length() == 0) {
			throw new ParseException("The parameter of #orparas directive can not be blank", this.location);
		}

		this.exprList = exprList;
	}

	private int addPara (SqlPara sqlPara, Object val, String paraName, Writer writer, int flag) {
		this.write(writer, flag > 0 ? " OR " : "");

		if (val == null) {
			this.write(writer, paraName + " IS NULL");
		} else {
			this.write(writer, paraName + " = ?");
			sqlPara.addPara(val);
		}

		return flag + 1;
	}

}
